package cn.edu.xmu.javaee.chapter2.beanlifecycle.bean;

import cn.edu.xmu.javaee.chapter2.beanlifecycle.config.ApplicationConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.MessageSource;

/**
 * @author: Ming Qiu
 * @date: Created in 21:14 2020/7/31s
 **/
@Slf4j
public class PaymentService implements BeanNameAware, BeanFactoryAware,
        ApplicationContextAware, InitializingBean, DisposableBean {

    private final MessageSource messageSource;
    private final PayAdaptor aliPayAdaptor;
    private final ApplicationConfig config;

    public PaymentService(MessageSource messageSource, PayAdaptor aliPayAdaptor, ApplicationConfig config) {
        this.messageSource = messageSource;
        this.aliPayAdaptor = aliPayAdaptor;
        this.config = config;
        log.info("PaymentService constructor");
    }

    private BeanFactory beanFactory;
    private ApplicationContext applicationContext;
    private String beanName;

    private String prompt;


    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
        log.warn(messageSource.getMessage("setBeanFactory",new Object[] {"PaymentService"}, "property setBeanFactory not exist", config.getLocale()));
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        log.warn(messageSource.getMessage("setApplicationContext",new Object[] {"PaymentService"}, "property setApplicationContext not exist", config.getLocale()));
    }

    @Override
    public void setBeanName(String name) {
        this.beanName = name;
        log.warn(messageSource.getMessage("setBeanName",new Object[] {"PaymentService"}, "property setBeanName not exist", config.getLocale()));
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.prompt = "hello";
        log.warn(messageSource.getMessage("afterPropertiesSet",new Object[] {"PaymentService"}, "property afterPropertiesSet not exist", config.getLocale()));
    }

    @Override
    public void destroy() throws Exception {
        log.warn(messageSource.getMessage("destroy",new Object[] {"PaymentService"}, "property destroy not exist", config.getLocale()));
    }

    public void myPostConstruct() {
        log.warn(messageSource.getMessage("myPostConstruct",new Object[] {"PaymentService"}, "property myPostConstruct not exist", config.getLocale()));
    }

    public void myPreDestory() {
        log.warn(messageSource.getMessage("myPreDestory",new Object[] {"PaymentService"}, "property myPreDestory not exist", config.getLocale()));
    }

    @Override
    public String toString() {
        return "PaymentService{" +
                "adaptor=" + this.aliPayAdaptor +
                '}';
    }
}

